package com.whfc.iot.gas.processor;

import com.whfc.common.constant.QueueConst;
import com.whfc.common.enums.DevicePlatform;
import com.whfc.common.enums.DeviceType;
import com.whfc.common.util.JSONUtil;
import com.whfc.env.dto.AppDeviceDustLogDTO;
import com.whfc.gas.entity.GasMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class GasUpMsgProcessorImpl implements GasUpMsgProcessor<GasMsg> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void processMsg(GasMsg reqMsg) {

        logger.info("处理gas数据,{}", reqMsg);

        AppDeviceDustLogDTO logDTO = new AppDeviceDustLogDTO();
        logDTO.setDeviceType(DeviceType.gas.getValue());
        logDTO.setPlatform(DevicePlatform.empaer.name());
        logDTO.setDeviceCode(reqMsg.getMac());
        logDTO.setDeviceId(reqMsg.getDeviceId());
        logDTO.setTime(reqMsg.getTime());
        logDTO.setCo(transform(reqMsg.getCo()));
        logDTO.setO2(transform(reqMsg.getO2()));
        logDTO.setH2s(transform(reqMsg.getH2s()));
        logDTO.setCh4(transform(reqMsg.getEx()));
        //入消息队列
        String textMsg = JSONUtil.toString(logDTO);
        try {
            amqpTemplate.convertAndSend(QueueConst.ENV_DUST_DATA, textMsg);
        } catch (Exception ex) {
            logger.error("", ex);
        }
    }

    private Long transform(Float value) {
        if (value == null) {
            return null;
        }
        return new BigDecimal(value).longValue();
    }
}
